﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Project_KhuyenMaiVang.DataProvider;
using Project_KhuyenMaiVang.Models;
using System.Collections;
using System.Data;
using System.Data.Sql;
using System.Data.SqlClient;

namespace Project_KhuyenMaiVang.DAO
{
    public class DangKyNhanTinKhuyenMaiDAO : MyDataProvider
    {
        // them email vao bang
        public bool insertEmail(string email)
        {
            bool isOK = true;
            try
            {
                string insertCommand = "INSERT INTO DangKyNhanTinKhuyenMai VALUES('" + email + "')";
                executeNonQuery(insertCommand);
            }
            catch
            {
                isOK = false;
            }

            return isOK;
        }

        // kiem lay email tuong ung trong ban dang ky
        public DangKyNhanTinKhuyenMaiDTO getEmail(string email)
        {
            DangKyNhanTinKhuyenMaiDTO dangKyNhanTinKhuyenMaiDTO = new DangKyNhanTinKhuyenMaiDTO();
            try
            {
                string strCommand = "SELECT* FROM DangKyNhanTinKhuyenMai WHERE Email = '" + email + "'";
                _Adapter= new SqlDataAdapter(strCommand, _Connection);
                DataSet dataset = new DataSet();
                _Adapter.Fill(dataset);
                DataTable dt = dataset.Tables[0];
                object ob = GetDataFromDataRow(dt, 0);
                dangKyNhanTinKhuyenMaiDTO = (DangKyNhanTinKhuyenMaiDTO)ob;
            }
            catch
            {
                dangKyNhanTinKhuyenMaiDTO = null;
            }

            return dangKyNhanTinKhuyenMaiDTO;
        }

        protected override object GetDataFromDataRow(DataTable dt, int i)
        {
            DangKyNhanTinKhuyenMaiDTO dangKyNhanTinKhuyenMaiDTO = new DangKyNhanTinKhuyenMaiDTO();
            dangKyNhanTinKhuyenMaiDTO.Email = dt.Rows[i]["Email"].ToString();

            return (object)dangKyNhanTinKhuyenMaiDTO;
        }
    }
}